-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local test_utils = require 'test_utils'
local lu = require 'luaunit'
local common    = require 'common'

require 'spdm.json_types.MeasurementSet'
require 'spdm.json_types.SPDMMeasurement'

local measurement_suit = {}
measurement_suit.__index = measurement_suit

local CHASSIS_ID = 1
local MDB_NAME = "SPDM_0"
local INTEGRITY_ID = 1

function measurement_suit.test_get_measurement_set(bus, test_data_dir)
    lu.assertEquals(
        test_utils.get_measurement_set_property(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, "Specification"),
        "DMTF"
    )
    lu.assertEquals(
        test_utils.get_measurement_set_property(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, "SummaryType"),
        "TCB"
    )
    lu.assertEquals(
        test_utils.get_measurement_set_property(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, "SummaryHashAlgorithm"),
        "TPM_ALG_SHA_256"
    )
    lu.assertEquals(
        test_utils.get_measurement_set_property(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, "SummaryHash"),
        "0101010101010101010101010101010101010101010101010101010101010101"
    )
end

local measurement_required = {
    ["HashAlgorithm"] = "TPM_ALG_SHA_256",
    ["MeasurementType"] = "FirmwareConfiguration",
    ["PartofSummaryHash"] = true,
    ["SecurityVersionNumber"] = ""
}

local mock_measurements = {
    [1] = "0000000000000000000000000000000000000000000000000000000000000000",
    [2] = "0000000000000000000000000000000000000000000000000000000000000001",
    [3] = "0000000000000000000000000000000000000000000000000000000000000002"
}

function measurement_suit.test_get_measurement(bus, test_data_dir)
    -- 取三组measurement
    for i = 1, 3 do
        lu.assertEquals(
            test_utils.get_measurement_property(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, i, "Index"),
            i
        )
        lu.assertEquals(
            test_utils.get_measurement_property(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, i, "Measurement"),
            mock_measurements[i]
        )
        for k, v in pairs(measurement_required) do
            lu.assertEquals(
                test_utils.get_measurement_property(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, i, k),
                v
            )
        end
    end
end

function measurement_suit.test_get_signed_measurement(bus, test_data_dir)
    -- 使用1、2、3三个槽位进行签名
    local measurement_indices = {1, 2, 3}
    local slot_id = 0
    local nonce = "0102030405060708090A0B0C0D0E0F100102030405060708090A0B0C0D0E0F10"
    local version, public_key, hash_algo, sign_algo, measurement =
        test_utils.call_get_signed_measurement(bus, CHASSIS_ID, MDB_NAME, INTEGRITY_ID, measurement_indices,
        slot_id, nonce)

    lu.assertEquals(version, "1.0")
    lu.assertEquals(hash_algo, "TPM_ALG_SHA_256")
    lu.assertEquals(sign_algo, "TPM_ALG_RSAPSS_2048")
    lu.assertEquals(public_key, "")

    local expect_measurement = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000" ..
        "0000000000000000000000000000000000000000000010101010101010101010101010101010101010101010101010101010101010101"
    lu.assertEquals(measurement, common.base64_encode(common.hex_to_bytes(expect_measurement)))
end

return measurement_suit